from pyecharts.charts import Pie
from pyecharts.options import TitleOpts, LabelOpts, TextStyleOpts

from utils import create_connection

conn = create_connection(database='hrs')
try:
    with conn.cursor() as cursor:
        cursor.execute('select dname, total from vi_dept_emp_count')
        pie_chart = Pie()
        pie_chart.add('', cursor.fetchall())
        pie_chart.set_global_opts(
            title_opts=TitleOpts(
                title='部门人数占比',
                pos_left='100',
                title_textstyle_opts=TextStyleOpts(
                    font_size=32,
                    font_family='FZJKai-Z03S',
                    color='darkgreen'
                )
            )
        )
        pie_chart.set_series_opts(
            label_opts=LabelOpts(formatter='{b}: {c} {d}%')
        )
        pie_chart.render()
finally:
    conn.close()
